package com.maben.easyui.controller;

import com.alibaba.fastjson.JSON;
import com.maben.easyui.controller.vo.UserVo;
import com.maben.easyui.pojo.User;
import com.maben.easyui.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户相关controller
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 去往用户列表页面
     */
    @RequestMapping("/ListView2")
    public ModelAndView toUserListView2(){
        return new ModelAndView("user/list2.html");
    }

    /**
     * 去往用户列表页面
     */
    @RequestMapping("/ListView")
    public ModelAndView toUserListView(){
        return new ModelAndView("user/list.html");
    }

    /**
     * 查询用户集合
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public List<UserVo> queryUserList(UserVo userVo)throws Exception{
        final List<UserVo> userVos = userService.queryUserListContainPage(userVo);
        System.out.println(JSON.toJSONString(userVos));
        return userVos;
    }
    @RequestMapping(value = "/list2",method = RequestMethod.POST)
    public Map<String,Object> queryUserList2(UserVo userVo)throws Exception{
        final Map<String,Object> result = new HashMap<>();
        final List<UserVo> userVos = userService.queryUserListContainPage(userVo);
        final Integer count = userService.queryUserCount(userVo);
        result.put("total",count);
        result.put("rows",userVos);
        result.put("footer",new ArrayList<Map<String,Object>>(){{
            add(new HashMap<String,Object>(){{
                put("id","页脚id");
                put("firstname",count);
            }});
        }});
        return result;
    }

    /**
     * 通过id获取用户信息
     */
    @RequestMapping("/{id}")
    public User queryUserById(@PathVariable("id") String id)throws Exception{
        if (StringUtils.isBlank(id)){
            return null;
        }
        return userService.queryUserById(id);
    }

    /**
     * 保存用户
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Map<String,Object> saveUser(UserVo userVo)throws Exception{
        Map<String,Object> result = new HashMap<>();
        result.put("success",true);
        userService.saveUser(userVo);
        return result;
    }

    /**
     * 物理删除用户
     */
    @RequestMapping(value = "remove",method = RequestMethod.POST)
    public Map<String,Object> removeUser(UserVo userVo)throws Exception{
        Map<String,Object> result = new HashMap<>();
        result.put("success",true);
        userService.removeUser(userVo);
        return result;
    }
}
